﻿String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
$(document).ready(function() {

    //--------------------建筑处理------------------
    $("#btnAddJZ1").bind("click", function() {

        var front = "";
        var last = "";
        var max = 0;
        var count = 0;
        //获取最大id
        $("tr[id^='jztr']").each(function() {
            //if ($(this).css("display") != "none") {
            front = $(this).attr("id");
            last = front.toString().substr(4, front.length - 4);
            if (Number(last) > max) {
                max = last;
                // alert(max);
            }
            count++;
            //}
        });
        $("a[id*='btnDelJZ']").show();
        var newid = Number(max) + 1;
        var lastNumber = count + 1;
        $("#tableJZ tr:eq(" + lastNumber + ")").after($("#jztr" + max).clone().attr("id", "jztr" + newid));
        $("#jztr" + newid + " input").each(function() {
            $(this).val(""); //清空数据
            $(this).css("background-color", "");
            //alert($(this).attr("id"));
        })
        $("#jztr" + newid + " select").each(function() {
            $(this).get(0).selectedIndex = 0; //初始
            $(this).css("background-color", "");
        })
        $("#jztr" + newid + " a").each(function() {
            var count = 0;
            $(this).attr("id", "btnDelJZ" + newid);
            $(this).bind("click", function() {
                $("tr[id^='jztr']").each(function() {
                    //if ($(this).css("display") != "none") {
                    count++;
                    //}
                });
                if (count > 1) {
                    if (count == 2) {
                        $("a[id*='btnDelJZ']").each(function() {
                            $(this).hide();
                        })
                    }
                    $("#jztr" + newid).remove();
                }
            })
        })
    });
    $("a[id*='btnDelJZ']").each(function() {
        $(this).bind("click", function() {
            var count = 0;
            $("tr[id^='jztr']").each(function() {
                //if ($(this).css("display") != "none") {
                count++;
                //}
            });
            if (count > 1) {
                if (count == 2) {
                    $("a[id*='btnDelJZ']").each(function() {
                        $(this).hide();
                    })
                }
                $(this).parent().parent().remove();
            }
        });
    });

    BindSYXZ();

    //---------------------------------------------------

    $("#ctl00_MainContent_CblSbType_0").live("click", function() {
        if ($(this).attr("checked") != "") {
            $("#ctl00_MainContent_CblSbType_1").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_2").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_3").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_4").removeAttr("checked");
            if ($("#ctl00_MainContent_ZXTable") != null && $("#ctl00_MainContent_ZXTable") != undefined) {
                HideZX();
            }
        }
    })
    $("#ctl00_MainContent_CblSbType_1").live("click", function() {
        if ($(this).attr("checked") != "") {
            $("#ctl00_MainContent_CblSbType_0").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_4").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_2").attr("checked", "checked");
            if ($("#ctl00_MainContent_ZXTable") != null && $("#ctl00_MainContent_ZXTable") != undefined) {
                HideZX();
            }
        }
        else {
            $("#ctl00_MainContent_CblSbType_2").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_3").removeAttr("checked");
        }
    })
    $("#ctl00_MainContent_CblSbType_4").live("click", function() {
        if ($(this).attr("checked") != "") {
            $("#ctl00_MainContent_CblSbType_1").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_2").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_3").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_0").removeAttr("checked");
            if ($("#ctl00_MainContent_ZXTable") != null && $("#ctl00_MainContent_ZXTable") != undefined) {
                HideZX();
            }
        }
    })
    $("#ctl00_MainContent_CblSbType_2").live("click", function() {
        if ($(this).attr("checked") != "") {
            $("#ctl00_MainContent_CblSbType_1").attr("checked", "checked");
            $("#ctl00_MainContent_CblSbType_0").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_4").removeAttr("checked");
        }
        else {
            if ($("#ctl00_MainContent_CblSbType_3").attr("checked") == "") {
                $("#ctl00_MainContent_CblSbType_1").removeAttr("checked");
            }
        }
    })
    $("#ctl00_MainContent_CblSbType_3").live("click", function() {
        if ($(this).attr("checked") != "") {
            $("#ctl00_MainContent_CblSbType_1").attr("checked", "checked");
            $("#ctl00_MainContent_CblSbType_4").removeAttr("checked");
            $("#ctl00_MainContent_CblSbType_0").removeAttr("checked");
            $("#ctl00_MainContent_HiddenField2").val("CL");
            if ($("#ctl00_MainContent_ZXTable") != null && $("#ctl00_MainContent_ZXTable") != undefined) {
                ShowZX();
            }
        }
        else {
            if ($("#ctl00_MainContent_CblSbType_2").attr("checked") == "") {
                $("#ctl00_MainContent_CblSbType_1").removeAttr("checked");
            }
            HideZX();
        }
    })

    //$("#ctl00_MainContent_DWJsdiv1_select").attr("href", "#");
    //$("#ctl00_MainContent_DWJsdiv1_remove").attr("href", "#");

});

function GetEventJZ() {
    $("#ctl00_MainContent_HiddenField2").val("JZ");
}
function GetEventDW() {
    $("#ctl00_MainContent_HiddenField2").val("DW");
}
function GetEventCG() {
    $("#ctl00_MainContent_HiddenField2").val("CG");
}
function GetEventDC() {
    $("#ctl00_MainContent_HiddenField2").val("DC");
}
function GetEventCL() {
    $("#ctl00_MainContent_HiddenField2").val("CL");
}

//使用性质验证
function xinzhi() {

    var yanzheng = true;
    $("input[type='checkbox'][id^='ctl00_MainContent_Control_DXMJ_']").each(function() {
        if ($(this).attr("checked") != "") {
            yanzheng = true;
            return false;
        }
        else {
            yanzheng = false;
        }
    });
    if (yanzheng == false) {
        $(",input[type='checkbox'][id^='ctl00_MainContent_Control_TSGC_']").each(function() {
            if ($(this).attr("checked") != "") {
                yanzheng = true;
                return false;
            }
            else {
                yanzheng = false;
            }
        });
    }
    if (yanzheng == false) {
        alert("请选择使用性质");
    }
    return yanzheng;
}

//工程类被
function leibei() {
    var yanzheng = true;
    $("input[type='checkbox'][id^='ctl00_MainContent_CblSbType']").each(function() {
        if ($(this).attr("checked") != "") {
            yanzheng = true;
            return false;
        }
        else {
            yanzheng = false;
        }
    });
    if (yanzheng == false) {
        alert("请选择工程类别");
    }
    return yanzheng;
}

//建筑验证
function jzdata(cgdc) {//1 表示存在储罐堆场 0 表示不存在
    var jzdata = "";      //建筑数据
    var yanzheng = true;  //验证结果
    var shifou = false;   //是否开启建筑验证
    var jzcount = 0;      //建筑数量
    if (cgdc == "1") {    //储罐堆场存在，行数大于2行，验证开启
        $("tr[id^='jztr']").each(function() {
            if ($(this).css("display") != "none") {
                jzcount++;
            }
        });
        if (jzcount == 1) {
            $("tr[id^='jztr']").each(function() {
                if ($(this).css("display") != "none") {
                    $(this).children("td").each(function() {
                        $(this).children("input").each(function() {
                            var id = $(this).attr("id").toString().Trim();
                            var subid = id.substr(id.search("txt"), id.length);
                            switch (subid) {
                                case "txtJzmc":
                                case "txtJzcs":
                                case "txtJzgd":
                                case "txtJzmj":
                                    if ($(this).val().toString().Trim() != "") {
                                        shifou = true;
                                    }
                                    break;
                            }
                        });
                    });
                }
            });
        }
        else {
            shifou = true;
        }
    }
    else {
        shifou = true;
    }
    if (shifou == true) {
        $("tr[id^='jztr']").each(function() {
            if ($(this).css("display") != "none") {
                $(this).children("td").each(function() {
                    $(this).children("input").each(function() {
                        var id = $(this).attr("id").toString().Trim();
                        var subid = id.substr(id.search("txt"), id.length);
                        var bl = true;
                        switch (subid) {
                            case "txtJzmc":
                            case "txtJzcs":
                            case "txtJzgd":
                            case "txtJzmj":
                                if ($(this).val().toString().Trim() == "") {
                                    $(this).css("background-color", "plum");
                                    yanzheng = false;
                                    bl = false;
                                }
                                else {
                                    //jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                        }
                        if (bl == true) {
                            switch (subid) {
                                case "txtJzgd":
                                    var mode = /^\d{1,6}(\.\d{1,2})?$/;
                                    if (!mode.test($(this).val())) {
                                        $(this).css("background-color", "plum");
                                        yanzheng = false;
                                    }
                                    else {
                                        jzdata += subid + "★" + $(this).val() + "☆";
                                        $(this).css("background-color", "");
                                    }
                                    break;
                                case "txtJzmj":
                                    var mode = /^\d{1,7}(\.\d{1,2})?$/;
                                    if (!mode.test($(this).val())) {
                                        $(this).css("background-color", "plum");
                                        yanzheng = false;
                                    }
                                    else {
                                        jzdata += subid + "★" + $(this).val() + "☆";
                                        $(this).css("background-color", "");
                                    }
                                    break;
                                case "txtZdmj":
                                case "txtFhfqzdmj":
                                case "txtFyfqzdmj":
                                    if ($(this).val().Trim() != "") {
                                        var mode = /^\d{1,7}(\.\d{1,2})?$/;
                                        if (!mode.test($(this).val())) {
                                            $(this).css("background-color", "plum");
                                            yanzheng = false;
                                        }
                                        else {
                                            jzdata += subid + "★" + $(this).val() + "☆";
                                            $(this).css("background-color", "");
                                        }
                                    }
                                    else {
                                        jzdata += subid + "★" + $(this).val() + "☆";
                                        $(this).css("background-color", "");
                                    }
                                    break;
                                case "txtFhfqsl":
                                case "txtFyfqsl":
                                    var mode = /^[0-9]{0,4}$/;
                                    if (!mode.test($(this).val())) {
                                        $(this).css("background-color", "plum");
                                        yanzheng = false;
                                    }
                                    else {
                                        jzdata += subid + "★" + $(this).val() + "☆";
                                        $(this).css("background-color", "");
                                    }
                                default:
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    break;
                            }
                        }
                    });
                    $(this).children("select").each(function() {
                        var id = $(this).attr("id").toString().Trim();
                        var subid = id.substr(id.search("drop"), id.length);
                        if (subid == "dropNhdj") {
                            if ($(this).get(0).selectedIndex == "0") {
                                yanzheng = false;
                                $(this).css("background-color", "plum");
                            }
                            else {
                                $(this).css("background-color", "");
                                $(this).children("option").each(function() {
                                    if ($(this).attr("selected") != "" && $(this).attr("value") != "#") {
                                        jzdata += subid + "★" + $(this).attr("value") + "◇" + $(this).text() + "☆";
                                    }
                                })
                            }
                        }
                        else {
                            $(this).children("option").each(function() {
                                if ($(this).attr("selected") != "") {
                                    if ($(this).attr("value") != "#") {
                                        jzdata += subid + "★" + $(this).attr("value") + "◇" + $(this).text() + "☆";
                                    }
                                    else {
                                        jzdata += subid + "★◇☆";
                                    }
                                }
                            })
                        }
                    });
                });
            }
        });
    }
    if (jzdata.length > 0) {
        jzdata = jzdata.substr(0, jzdata.length - 1);
    }
    if (yanzheng == false) {
        alert("建筑数据格式不正确");
    }
    $("#ctl00_MainContent_HiddenField1").val(jzdata);
    return yanzheng;
}


//保存数据并验证
function GetEventSave() {
    var yanzheng = true;
    if ($("#ctl00_MainContent_dropDw").size() > 0) {
        if ($("#ctl00_MainContent_dropDw").get(0).selectedIndex == 0) {
            yanzheng = false;
            alert("请选择管辖单位");
        }
    }
    if (yanzheng == true) {
        var YjkgTime = $("#ctl00_MainContent_txtYjkgTime").val();
        var YjjgTime = $("#ctl00_MainContent_txtYjjgTime").val();
        if (YjjgTime != "" && YjkgTime != "") {
            if (YjjgTime <= YjkgTime) {
                yanzheng = false;
                alert("预计竣工时间必须大于预计开工时间");
            }
        }
    }

    if (yanzheng == true) {
        yanzheng = xinzhi(); //验性质
    }
    if (yanzheng == true) {

        yanzheng = leibei(); //验类别
    }
    if (yanzheng == true) {
        var szwz = $("#ctl00_MainContent_CGdivCG1_txtSzwznr").val();
        var dccl = $("#ctl00_MainContent_DCdivDC1_txtClnr").val();
        if (szwz == "" && dccl == "") {
            yanzheng = jzdata("0"); //验建筑
        }
        else {
            yanzheng = jzdata("1"); //验建筑
        }
    }
    $("#ctl00_MainContent_HiddenField2").val("Save");
    return yanzheng;
}

function DelDC() {
    if (confirm('你确定要删除最后一个堆场')) {
        GetEventDC();
        return true;
    }
    else
        return false;
}
function DelCG() {
    if (confirm('你确定要删除最后一个储罐')) {
        GetEventCG();
        return true;
    }
    else
        return false;
}
function DelDW() {
    if (confirm('你确定要删除该单位')) {
        GetEventDW();
        return true;
    }
    else
        return false;
}
function DelCL() {
    if (confirm('你确定要删除该材料')) {
        GetEventCL();
        return true;
    }
    else
        return false;
}

function DelJZ() {
    if (confirm('你确定要删除该建筑')) {
        GetEventJZ();
        return true;
    }
    else
        return false;
}


function RemoveCheck() {
    $("input[type='checkbox'][id*='CblSbType']").each(function() {
        $(this).removeAttr("onclick");
    });
}

function HideZX() {
    $("#ctl00_MainContent_UpdatePanelZXCL").hide();
    $("#ctl00_MainContent_R_txtxzJzcs").removeAttr("validationGroup");
    $("#ctl00_MainContent_R_txtYyyt").removeAttr("validationGroup");
    $("#ctl00_MainContent_R_txtZxmj").removeAttr("validationGroup");
    $("#ctl00_MainContent_R_txtZxcs").removeAttr("validationGroup");
    $("#ctl00_MainContent_RegularExpressionValidator2").removeAttr("validationGroup");
    $("#ctl00_MainContent_RegularExpressionValidator3").removeAttr("validationGroup");
    $("span[style='color:Red;display:none;']").each(function() {
        $(this).validationGroup = "";
    });
}

function ShowZX() {
    $("#ctl00_MainContent_UpdatePanelZXCL").show();
    $("#ctl00_MainContent_R_txtxzJzcs").attr("validationGroup", "SBBGroup");
    $("#ctl00_MainContent_R_txtYyyt").attr("validationGroup", "SBBGroup");
    $("#ctl00_MainContent_R_txtZxmj").attr("validationGroup", "SBBGroup");
    $("#ctl00_MainContent_R_txtZxcs").attr("validationGroup", "SBBGroup");
    $("#ctl00_MainContent_RegularExpressionValidator2").attr("validationGroup", "SBBGroup");
    $("#ctl00_MainContent_RegularExpressionValidator3").attr("validationGroup", "SBBGroup");
    $("span[style='color:Red;display:none;']").each(function() {
        $(this).validationGroup = "SBBGroup";
    });
}

function popWin(selectAction, Title) {
    setTimeout(
                      function() {
                          ymPrompt.win({ message: selectAction + "&DWMC=" + $("#ctl00_MainContent_DWJsdiv1_txtJSDW").val(),
                              width: 760,
                              height: 500,
                              title: Title,
                              handler: null,
                              maxBtn: true,
                              minBtn: true,
                              iframe: true
                          });
                      }, 50);
    return false;
}

function removeDW() {
    $("#ctl00_MainContent_HidDWID").val("");
    $("#ctl00_MainContent_DWJsdiv1_select").html("单位关联");
    $("#ctl00_MainContent_DWJsdiv1_remove").css("display", "none");
    $("#ctl00_MainContent_selectTable").css("display", "");
    $("#ctl00_MainContent_selectTable1").css("display", "none");
    return false;
}

function Getbh(p1, p2) {
    var obj = document.getElementById("ctl00_MainContent_DWJsdiv1_" + p2);
    var WinElementPos = getWinElementPos(obj);
    var val = document.getElementById(p1);
    val.style.display = "inline";
    val.style.left = (parseInt(WinElementPos.x) - 25).toString() + "px";
    val.style.top = (parseInt(WinElementPos.y) - 107).toString() + "px";
}
function yincan(p1) {
    var val = document.getElementById(p1);
    val.style.display = "none";
}